package com.example.interview;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import org.json.JSONException;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.LinkedList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;

public class SecondFragment extends Fragment implements FirstFragment.onItemClickedListener {
    final RecyclerView.OnScrollListener onScrollListener;

    public SecondFragment(RecyclerView.OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_second, container, false);
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        doRequest(view);
    }

    private FirstFragment.onItemClickedListener onItemClickedListener;

    public void doRequest(View view) {
        final List<Item> itemList = new LinkedList<>();
        new Thread(() -> {
            try {
                Utils.getTeachers(itemList);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }).start();
        synchronized (itemList) {
            try {
                itemList.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setAdapter(new RecyclerView.Adapter() {
            @NonNull
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                ImageView itemView = new ImageView(parent.getContext());
                // TODO: 2020/9/7 在正方形中显示长方形图片的上半部分，避免人像头部显示到图像外部
                itemView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                itemView.setLayoutParams(new RecyclerView.LayoutParams(parent.getWidth(), parent.getWidth()));
                return new RecyclerView.ViewHolder(itemView) {
                };
            }

            @Override
            public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
                holder.itemView.setTag(itemList.get(position));
                new Thread(() -> {
                    try {
                        Utils.getImage((ImageView) holder.itemView, itemList.get(position).image);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();
            }

            @Override
            public int getItemCount() {
                return itemList.size();
            }
        });
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        recyclerView.addOnScrollListener(onScrollListener);
        onItemClickedListener = item -> {
            for (int i = 0; i < itemList.size(); ++i) {
                if (itemList.get(i).teacherId == item.teacherId) {
                    recyclerView.scrollToPosition(i);
                }
            }
        };
    }

    @Override
    public void onItemClicked(Item item) {
        if (onItemClickedListener != null) {
            onItemClickedListener.onItemClicked(item);
        }
    }
}